<?php
/**
 * 新建档案
 */
apf_require_controller("Abstract");
class Flow_AddUserController extends AbstractController {

    private $error_item;//验证错误项
    public function handle_request() {
        /*** 验证角色 对于所有行政招聘 */
        $role = $this->user_info['role_list'];
        if(!isset($role[Const_Flow::ROLE_RECRUIT])){
            $this->response->redirect('/flow/');
        }
        $params = $this->request->get_parameters();

        //页面提交
        if($this->request->is_post_method()){
            ##城市编辑权限的才可提交  城市+入职+第一步+员工档案录入
            $rights = $this->_check_auth($params['work_city_id'], Const_Flow::FLOW_TYPE_ENTRY, Const_Flow::FLOW_ENTRY_1);
            if($rights){
                ## 验证必填项
                if($this->_check_input_data($params)){

                    if($params['submit']==1){//到待办事项
                        //入职处理
                        $this->_save_or_update_entry($params);
                        if(intval($params['entry_id'])){
                            $this->show_tips('EDIT_ENTRY_SUCCESS',"/flow/?do=process",Const_Tip::SUCCESS);
                        }else{
                            $this->show_tips('ADD_ENTRY_SUCCESS',"/flow/?do=process",Const_Tip::SUCCESS);
                        }
                    }elseif($params['submit']==2){//提示前端添加成功,并继续添加
                        //入职处理
                        $this->_save_or_update_entry($params);
                        if(intval($params['entry_id'])){
                            $this->request->set_attribute('notice',"edit");
                        }else{
                            $this->request->set_attribute('notice',"add");
                        }
                        $params = array();
                    }elseif($params['submit']==3){//取消入职
                        //取消入职处理
                        $this->_no_entry($params);
                        $this->show_tips('NO_ENTRY_SUCCESS',"/flow/?do=process",Const_Tip::SUCCESS);
                    }
                }
            }else{//职位清空
                $this->error_item['job_id'] = '请选择担任职位';
                $params['work_city_id']= "";
                $params['job_name']= "";
                $params['job_id']= "";
            }
        }else{//页面编辑
            $entry_id = intval($params['id']);
            //user info
            $user = $this->_get_bll_flow_user()->get_user_by_id($entry_id);
            //有该用户的编辑权限
            if($user && in_array($user['work_city_id'], $role[Const_Flow::ROLE_RECRUIT])){
                /*** 是否进行到下一步*/
                if($this->_check_can_operate($user)){
                    $params = $this->_get_user_info($user);
                }else{
                    $this->show_tips('ERROR_PARAM',"/flow/?do=process",Const_Tip::ERROR);
                }
            }
        }
        //用户类型
        $this->request->set_attribute('user_type',$this->_get_bll_hroa()->get_all_user_type());
        //获取合同签约城市
        $citys = $this->_get_allow_city();
        $this->request->set_attribute('agree_city',$citys);
        $this->request->set_attribute('operator_city_id',$this->user_info['work_city_id']);
        // 错误日志
        $this->request->set_attribute('user',$params);
        $this->request->set_attribute('error',$this->error_item);
        return "Flow_AddUser";
    }
    /**
     * 判断是否进行到下一步
     * @param unknown_type $entry_id
     */
    private function _check_can_operate($entry){
        return $this->_get_bll_flow_user()->check_recruit_can_operate($this->_get_bll_flow_info(), $this->_get_bll_flow_log(), $entry);
    }
    /**
     * 获取可编辑城市
     * @return array
     */
    private function _get_allow_city(){
        $citys = $this->_get_bll_hroa()->get_city_list(true);
        $role = $this->user_info['role_list'];
        $result = array();
        foreach ($role[Const_Flow::ROLE_RECRUIT] as $val){
            if(isset($citys[$val])){
                $result[$val] = $citys[$val];
            }
        }
        return $result;
    }
    /**
     * 获取编辑信息
     * @param array $user
     */
    private function _get_user_info($user){
        //job
        $job_dep = $this->_get_bll_hroa()->get_job_dep_info($user['job_id']);

        $citys = $this->_get_bll_hroa()->get_city_list(false);
        $business = $this->_get_bll_hroa()->get_business_list();
        $user['job_name'] = $job_dep['position_name'];
        $user['job_name'] .= ' - '.$citys[$user['work_city_id']]['city_name'].$business[$job_dep['bu_id']]['bu_name'].$job_dep['department_name'];

        //leader
        $leader = $this->_get_bll_hroa()->get_user_base_info($user['parent_id'],true);
        $user['parent_name'] = $leader['real_name'];
        $user['parent_name'] .= ' - '.$leader['position_name'];

        //date
        $user['entry_date'] = date('Y-m-d',strtotime($user['entry_date']));
        return $user;
    }
    /**
     * 入职处理
     * @param array $params
     */
    private function _save_or_update_entry($params){
        //插入到数据库
        $entry_id = intval($params['entry_id']);
        if($entry_id){//编辑
            $data = $this->_build_post_data($params);
            $this->_get_bll_flow_user()->update_flow_user($entry_id,$data);
            //更新名字
            $flow = $this->_get_bll_flow_info()->get_flow_by_uid($entry_id);
            $data_flow = array('user_name'=>$data['real_name'],'work_city_id'=>$data['work_city_id'],'effect_date'=>$data['re_entry_date']);
            $this->_get_bll_flow_info()->update_flow_entry($flow['flow_id'],$data_flow);
            //更新log的生效时间
            $this->_get_bll_flow_log()->update_log_info($flow['flow_id'],$data['real_name'],$data['work_city_id'],$data['re_entry_date']);
        }else{//添加
            $data = $this->_build_post_data($params);
            $data['user_code'] = $this->_get_bll_hroa()->get_user_code($data['user_type'], $data['city_id'],false);
            $entry_id = $this->_get_bll_flow_user()->add_flow_user($data);
            //添加代办事项
            $data_flow = $this->_build_flow_info($entry_id,$data);
            $flow_id = $this->_get_bll_flow_info()->insert_flow($data_flow);
            //记录到log
            $data_log = $this->_build_operate_log($flow_id,$entry_id,$data);
            $this->_get_bll_flow_log()->save_log($data_log);
        }
    }
    /**
     * 取消入职处理
     * @param array $params
     */
    private function _no_entry($params){
        //flow 取消
        $entry_id = intval($params['entry_id']);
        if(!$entry_id){
            return false;
        }
        //更新user
        $data = array('is_entry'=>Const_Flow::ENTRY_CANCEL,'last_update'=>date('Y-m-d H:i:s'));
        $this->_get_bll_flow_user()->update_flow_user($entry_id,$data);
        //同步数据到hroa
        $this->_get_bll_flow_user()->from_entry_to_user($entry_id,array('zp'=>true),$this->_get_bll_hroa());
        //更新代办事项
        $flow = $this->_get_bll_flow_info()->get_flow_by_uid($entry_id);
        $this->_get_bll_flow_info()->update_flow_cancel($flow['flow_id']);
        //记录到log  不做log处理
        /* $data_log = $this->_build_operate_log($flow['flow_id'],$entry_id,$params);
        $data_log['flow_opt'] = intval(Const_Flow::PHASE_6);
        $data_log['effect_date'] = intval(date('Ymd',strtotime($data_log['effect_date'])));
        $this->_get_bll_flow_log()->save_log($data_log); */
    }
    /**
     * 构建日志数据
     * @return array()
     */
    private function _build_operate_log($flow_id,$entry_id,$data){
        $result = array(
                'flow_stage'=>intval(Const_Flow::FLOW_ENTRY_1),//第一阶段，档案录入
                'flow_type'=>intval(Const_Flow::FLOW_TYPE_ENTRY),//入职流程
                'flow_opt'=>intval(Const_Flow::PHASE_1),//入职-新建员工档案
                'user_id' =>intval($entry_id),
                'opt_user_id'=>intval($this->get_user_id()),//操作人id
                'role_type'=>intval(Const_Flow::ROLE_RECRUIT),//行政招聘
                'work_city_id'=>intval($data['work_city_id']),
                'flow_id'=>intval($flow_id),
                'operate_time'=>date('Y-m-d H:i:s'),
                'user_name'=>$data['real_name'],
                'effect_date'=>$data['entry_date']
                );
        return $result;
    }
    /**
     * 构建待办事项数据
     * @param array $data
     */
    private function _build_flow_info($entry_id,$data){
        $result = array(
                'user_id'=>$entry_id,
                'user_name'=>$data['real_name'],
                'flow_type'=>Const_Flow::FLOW_TYPE_ENTRY,//入职流程
                'flow_stage'=>Const_Flow::FLOW_ENTRY_1,//第一阶段，档案录入(now)
                'flow_opt'=>Const_Flow::PHASE_1,//入职-新建员工档案(now)
                'flow_role_list'=>Const_Flow::ROLE_ADMIN.','.Const_Flow::ROLE_IT,//下一步，行政管理,IT管理(next)
                'work_city_id'=>$data['work_city_id'],
                'effect_date'=>$data['entry_date'],
                'now_status'=>Const_Flow::FLOW_STATUS_DOING,//事项开始进行(now)
                'last_update'=>date('Y-m-d H:i:s'),
                );
        return $result;
    }
    /**
     * 构建入职数据
     */
    private function _build_post_data($params){
        $result = array(
                'real_name'=>trim($params['real_name']),
                'english_name'=>trim($params['english_name']),
                'sex'=>intval($params['sex']),
                'cellphone'=>trim($params['cellphone']),
                'identity_type'=>intval($params['identity_type']),
                'identity_code'=>trim($params['identity_code']),
                'entry_date'=>intval(date('Ymd',strtotime(trim($params['entry_date'])))),
                're_entry_date'=>intval(date('Ymd',strtotime(trim($params['entry_date'])))),
                'city_id'=>intval($params['city_id']),
                'work_city_id'=>intval($params['work_city_id']),
                'user_type'=>intval($params['user_type']),
                'job_id'=>intval($params['job_id']),
                'parent_id'=>intval($params['parent_id']),
                'parent_job_id'=>intval($params['parent_job_id']),
                );
        return $result;
    }
    /**
     * post 数据验证
     */
    private function _check_input_data($params){
        $boolean = true;
        //必填项验证
        if(!trim($params['real_name'])){//用户名
            $this->error_item['real_name'] = '请输入姓名';
            $boolean = false;
        }
        if(!trim($params['english_name']) || !preg_match("/[a-zA-Z .]{1,20}/", trim($params['english_name']))){
            $this->error_item['english_name'] = "您的英文名输入不正确";
            $result = false;
        }
        if(!trim($params['cellphone']) || !preg_match("/^0?(13[0-9]|15[0-35-9]|18[0236789]|14[57])[0-9]{8}$/", trim($params['cellphone']))){
            $this->error_item['cellphone'] = "手机号码输入不正确";
            $result = false;
        }
        if(!trim($params['identity_code'])){//证件号
            $this->error_item['identity_code'] = '请输入证件号';
            $boolean = false;
        }
        if(!trim($params['entry_date'])){//报到日期
            $this->error_item['entry_date'] = '请输入报到日期';
            $boolean = false;
        }
        if(!intval($params['city_id'])){//签约城市
            $this->error_item['city_id'] = '请选择签约城市';
            $boolean = false;
        }
        if(!intval($params['user_type'])){//员工类型
            $this->error_item['user_type'] = '请选择员工类型';
            $boolean = false;
        }
        if(!intval($params['job_id'])){//担任职位
            $this->error_item['job_id'] = '请选择担任职位';
            $boolean = false;
        }
        if(!intval($params['parent_id'])){//直属上级
            $this->error_item['parent_id'] = '请选择直属上级';
            $boolean = false;
        }
        return $boolean;
    }
    /**
     * 入职bll
     * @return Bll_Flow_User
     */
    private function _get_bll_flow_user(){
        if(!$this->bll_flow_user ){
            apf_require_class('Bll_Flow_User');
            $this->bll_flow_user = new Bll_Flow_User();
        }
        return $this->bll_flow_user;
    }
    /**
     * 待办事项bll
     * @return Bll_Flow_Info
     */
    private function _get_bll_flow_info(){
        if(!$this->bll_flow_info ){
            apf_require_class('Bll_Flow_Info');
            $this->bll_flow_info = new Bll_Flow_Info();
        }
        return $this->bll_flow_info;
    }
    /**
     * 操作日志
     * @return Bll_Flow_Log
     */
    private function _get_bll_flow_log(){
        if(!$this->bll_flow_log ){
            apf_require_class('Bll_Flow_OperateLog');
            $this->bll_flow_log = Bll_Flow_OperateLog::get_instance();
        }
        return $this->bll_flow_log;
    }
    /**
     * hroa interface bll
     * @return Bll_Hr_Property
     */
    private function _get_bll_hroa(){
        if(!$this->bll_hroa ){
            apf_require_class('Bll_Hr_OutInterface');
            $this->bll_hroa = new Bll_Hr_OutInterface();
        }
        return $this->bll_hroa;
    }
}